namespace Billing
{
    using Billing.Controls;
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using Microsoft.Security.Application;

    public class UserProfile : Page
    {
        protected Button btnAdd;
        private DataTable dt;
        protected GridView gvCustomView;
        protected ucPageTitle ucPageTitle1;
        protected Billing.ucSearch ucSearch;
        protected ucUserProfile ucUserProfile1;
        protected ucUserProfile ucUserProfile2;
        protected UpdatePanel UpdatePanel1;
        protected UpdatePanel UpdatePanel2;
        protected UpdatePanel UpdatePanel3;
        protected UpdatePanel UpdatePanel4;
        protected UpdatePanel UpdatePanel5;
        protected UpdateProgress UpdateProgress1;

        private void BindData()
        {
            this.GetDataSource();
            if (this.dt != null)
            {
                this.gvCustomView.DataSource = this.dt;
                this.gvCustomView.DataBind();
            }
            if ((this.gvCustomView.Rows.Count > 0) && !this.Page.IsPostBack)
            {
                this.ucSearch.SearchCriteria = ClsUtilityHelper.BindGridData(this.gvCustomView);
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            ClsUtilityHelper.OpenJQueryDialog("#dialogSave", this);
        }

        public DataTable getData()
        {
            ClsUserProfile cls = new ClsUserProfile();
            DataView dv = new DataView(cls.SelectAll());
            if ((this.ViewState["Sort"] != null) && (this.ViewState["Sort"].ToString() != ""))
            {
                dv.Sort = this.ViewState["Sort"].ToString();
            }
            return dv.ToTable();
        }

        private void GetDataSource()
        {
            if (this.ucSearch.FilterDT != null)
            {
                this.dt = this.ucSearch.FilterDT;
            }
            else if (this.dt == null)
            {
                this.dt = this.getData();
            }
        }

        private void GetPermission()
        {
            ClsACLHelper cls = new ClsACLHelper();
            string pageName = this.Page.ToString().Substring(4, this.Page.ToString().Substring(4).Length - 5) + ".aspx";
            cls.SetPermission(LoginUser.TreeMenu, pageName);
            this.btnAdd.Visible = cls.Create0;
            this.ucUserProfile1.Delete0 = false;
            this.ucUserProfile2.Delete0 = cls.Delete0;
            this.ucUserProfile2.Update0 = cls.Update0;
            if (!cls.Read0)
            {
                base.Response.Redirect("logout.aspx",true);
            }
        }

        protected void gvCustomView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gvCustomView.PageIndex = e.NewPageIndex;
            DataView dv = new DataView(this.ucSearch.FilterDT);
            if ((this.ViewState["Sort"] != null) && (this.ViewState["Sort"].ToString() != ""))
            {
                dv.Sort = this.ViewState["Sort"].ToString();
            }
            this.ucSearch.FilterDT = dv.ToTable();
            this.gvCustomView.DataSource = this.ucSearch.FilterDT;
            this.gvCustomView.DataBind();
        }

        protected void gvCustomView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToString().ToLower() == "editdata")
            {
                this.ucUserProfile2.EditData = new ClsUserProfile { ID = Convert.ToInt64(e.CommandArgument) }.Select();
                ClsUtilityHelper.OpenJQueryDialog("#dialogUpdate", this);
            }
        }

        protected void gvCustomView_Sorting(object sender, GridViewSortEventArgs e)
        {
            DataView dv = new DataView(this.ucSearch.FilterDT) {
                Sort = e.SortExpression + " " + this.ViewState["SortDirection"].ToString()
            };
            this.ViewState["Sort"] = dv.Sort;
            this.ucSearch.FilterDT = dv.ToTable();
            if (this.ViewState["SortDirection"].ToString() == "ASC")
            {
                this.ViewState["SortDirection"] = "DESC";
            }
            else
            {
                this.ViewState["SortDirection"] = "ASC";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.Session["BUID"] == null)
            {
                base.Response.Redirect("default.aspx",true);
            }
            this.ucUserProfile2.DialogName = "#dialogUpdate";
            this.ucUserProfile1.DialogName = "#dialogSave";
            if (!this.Page.IsPostBack)
            {
                this.ViewState["SortDirection"] = "ASC";
                this.BindData();
            }
            this.GetPermission();
        }

        protected void Page_LoadComplete(object sender, EventArgs e)
        {
            this.BindData();
        }
    }
}
